﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Linq.Expressions;

namespace MVVMFramework.Utilities
{
    public class PropertyChangedHelper
    {
        public static void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression, object sender, PropertyChangedEventHandler ev)
        {
            if (ev == null)
                return;

            if (propertyExpression.Body.NodeType == ExpressionType.MemberAccess)
            {
                var memberExpr = propertyExpression.Body as MemberExpression;
                string propertyName = memberExpr.Member.Name;
                PropertyChangedHelper.RaisePropertyChangedInternal(propertyName, sender, ev);
            }
        }

        public static void RaisePropertyChanged(string property, object sender, PropertyChangedEventHandler ev)
        {
            if (ev == null)
                return;

            PropertyChangedHelper.RaisePropertyChangedInternal(property, sender, ev);
        }

        private static void RaisePropertyChangedInternal(string property, object sender, PropertyChangedEventHandler ev)
        {
            ev(sender, new PropertyChangedEventArgs(property));
        }
    }
}
